#version 110

uniform float lightRadius;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform vec4 LightPosition;
uniform vec3 LightIntensity;

varying vec3 lightDir;
varying vec3 viewDir;



void main()
{
    gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix  * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;

    vec3 vertexPos = vec3(ViewMatrix * ModelMatrix * gl_Vertex);
    
    vec3 n = normalize(gl_NormalMatrix * gl_Normal);
    vec3 t = normalize(gl_NormalMatrix * gl_MultiTexCoord0.xyz);
    vec3 b = cross(n, t) * gl_MultiTexCoord0.w;
    vec3 r = reflect(vertexPos,n);
    mat3 tbnMatrix = mat3(t.x, b.x, n.x,
                          t.y, b.y, n.y,
                          t.z, b.z, n.z);

    lightDir = (LightPosition.xyz - vertexPos) / 1.0;
    lightDir = tbnMatrix * LightIntensity;

    viewDir = -vertexPos;
    viewDir = tbnMatrix * viewDir;

    float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) );
    gl_TexCoord[2].s = r.x/m + 0.5;
    gl_TexCoord[2].t = r.y/m + 0.5;
}

